<?php

include '../templateSys/SQL.php';
include '../templateSys/Settings.php';
include '../templateSys/manager_util.php';
include '../templateSys/Admin.php';

if (isset($_GET['logout'])) {
    session_start();
    unset($_SESSION['SESS_MEMBER_ID']);
    session_destroy();
    header("location: " . Settings::$rootPath . "" . Settings::$manager . "?action=" . Settings::$logOutPage);
    exit();
}

if (isset($_POST['login'])) {
    global $link;
    $link = getLink();
    login($_POST['email'], $_POST['pass']);
    mysql_close($link);
    exit();
}

if (isset($_POST['register'])) {
    global $link;
    $link = getLink();
    if ($_POST["pass"] == $_POST["check"]) {
        $query = sprintf(SQL::$insert_user, mysql_real_escape_string($_POST["email"]), mysql_real_escape_string(hashByMatu($_POST["email"], $_POST["pass"])), mysql_real_escape_string($_POST["email"]), 'user');
        $result = mq($query,$link);
        if ($result){
            login($_POST['email'], $_POST['pass']);
        }else{
            header("location: " . Settings::$rootPath . Settings::$manager . "?action=" . Settings::$registerPage . "&msg=emailExist");
        }
    }else{
        header("location: " . Settings::$rootPath . Settings::$manager . "?action=" . Settings::$registerPage . "&msg=kontrolaHesla");
    }
    mysql_close($link);
    exit();
}
?>
